package com.wm.service.imp;

import java.util.ArrayList;
import java.util.List;

import com.wm.bean.Collect;
import com.wm.dao.CollectDAO;
import com.wm.dao.ResMenuDAO;
import com.wm.dao.RestaurantDAO;
import com.wm.service.CollectService;

/**
 * @author yangbo
 *
 * 2013-1-6
 */
public class CollectServiceImp implements CollectService {
	
	private CollectDAO collectDAO;
	private RestaurantDAO restaurantDAO;
	private ResMenuDAO resMenu;

	public ResMenuDAO getResMenu() {
		return resMenu;
	}

	public void setResMenu(ResMenuDAO resMenu) {
		this.resMenu = resMenu;
	}

	public RestaurantDAO getRestaurantDAO() {
		return restaurantDAO;
	}

	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}

	public CollectDAO getCollectDAO() {
		return collectDAO;
	}

	public void setCollectDAO(CollectDAO collectDAO) {
		this.collectDAO = collectDAO;
	}

	public Collect findCollectById(long id) {
		Collect c = this.getCollectDAO().findCollectById(id);
		if(c!=null){
			c.setRestaurant(this.getRestaurantDAO().findRestaurantByIdValidIsShow(c.getObjId(),1,1));
		}
		return c;
	}

	public List findCollectByUserIdObjType(long userId,int objType){
		List l =  this.getCollectDAO().findCollectByUserIdObjType(userId,objType);
		List newList = new ArrayList();
		if(l!=null){
			for(int i=0;i<l.size();i++){
				Collect c = (Collect)l.get(i);
				if(objType==0){
					c.setRestaurant(this.getRestaurantDAO().findRestaurantByIdValidIsShow(c.getObjId(),1,1));
					if(c.getRestaurant()!=null){
						newList.add(c);
					}
				}
				else if(objType==1){
					c.setResMenu(this.getResMenu().findResMenuById(c.getObjId()));
					if(c.getResMenu()!=null){
						newList.add(c);
					}
				}
			}
		}
		return newList;
	}

	public void removeCollect(long id) {
		Collect c = this.getCollectDAO().findCollectById(id);
		if(c!=null){
			this.getCollectDAO().removeCollect(c);
		}
	}
	
	public void removeCollect(Collect c){
		this.getCollectDAO().removeCollect(c);
	}

	public Collect saveCollect(Collect c) {
		return this.getCollectDAO().saveCollect(c);
	}
	
	public Collect findCollectByUserIdObjIdObjType(long userId,long objId,int objType){
		return this.getCollectDAO().findCollectByUserIdObjIdObjType(userId, objId, objType);
	}
	
	public List findCollectByUserIdObjTypeFirstResultMaxResults(long userId,int objType,String orderby,int ascOrDesc,int firstResult,int maxResults){
		List l =  this.getCollectDAO().findCollectByUserIdObjTypeFirstResultMaxResults(userId, objType, orderby, ascOrDesc, firstResult, maxResults);
		List newList = new ArrayList();
		if(l!=null){
			for(int i=0;i<l.size();i++){
				Collect c = (Collect)l.get(i);
				if(objType==0){
					c.setRestaurant(this.getRestaurantDAO().findRestaurantByIdValidIsShow(c.getObjId(),1,1));
				}
				else if(objType==1){
					c.setResMenu(this.getResMenu().findResMenuById(c.getObjId()));
				}
			}
		}
		return l;
	}
}
